<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Group extends CI_Controller {
            public function __construct(){
                        parent::__construct();
                        $this->load->model(array("groupmodel"));
                }

	public function index()
	{
		
    $data=array();
		load($this->db,$this->session,$this->load,"group","index",$data);
	}


             public function get_group(){
               $groups=$this->groupmodel->get_groups();
               $str=json_encode($groups);
               echo $str;	

             }


             public function add(){
             	$data=array();
             	$functions=read_function($this->db,'-1');
             	$data['functions']=$functions;
                  load($this->db,$this->session,$this->load,"group","add",$data);
             }


           public function adddb(){
           	 $name=$this->input->post("name");
	     $functions=$this->input->post("functions");
	     $a=explode("|", $functions);
	     $temp=0;
	    $arr=array("name"=>$name);
	    if($this->db->insert("role",$arr)){
	    $id=$this->db->insert_id();
	    foreach ($a as $key => $value) {
	      $arr=array("function_id"=>$value,"role_id"=>$id);
	      if($this->db->insert("function_to_role",$arr)){

	      }else{$temp=1;}
	    }
	    }else{$temp=1;}

	    if($temp==0){
	    	$url=site_url()."index.php/group/index";
	         redirect($url);
	    }else{
	     
	    }

           }


             public function edit(){
             	$data=array();
             	$id=$_GET["id"];
              
             	$sql="select * from role where id='$id'";
             	$query=$this->db->query($sql);
                         $row=$query->row();
                         $data['name']=$row->name;
                         
                        $roleid=$id;
                        $functions=read_checked_function($this->db,"-1",$roleid);
             	$data['functions']=$functions;
             	$data['id']=$id;
                      load($this->db,$this->session,$this->load,"group","edit",$data);


             }


             public function update(){
             	 $id=$this->input->post("id");
		   $name=$this->input->post("name");
		   $functions=$this->input->post("functions");
		   $sql="update role set name='$name' where `id`='$id'";
		   $query=$this->db->query($sql);
		   $sql="delete from function_to_role where role_id='$id'";
		   if($query=$this->db->query($sql)){
		       $a=explode("|", $functions);
		         $temp=0;

		        $id=$id;
		        foreach ($a as $key => $value) {
		          $arr=array("function_id"=>$value,"role_id"=>$id);
		          if($this->db->insert("function_to_role",$arr)){

		          }else{$temp=1;}
		        }


		        if($temp==0){
		          $url=site_url()."index.php/group/index";
	                       redirect($url);
		        }else{
		          
		        }
		   }else{  
		   }

             }

             public function del(){
            
              $id = intval($_REQUEST['id']);
                $tmp=0;
              
               	
               	  $sql="delete from function_to_role where role_id='$id'";
                          if($this->db->query($sql)){
                               $sql="delete from role where id='$id'";
                                if($this->db->query($sql)){
                                  success();
                                }else{
                                	$tmp=1;
                                }

                              }else{

                              }

             }


}
